// --------------------评论列表-------------------------
let id = new URLSearchParams(location.search).get('id')
function pinglun() {
  
$.ajax({
  url: 'http://localhost:8888/api/articles/' + id + '/comments',
  success: function (res) {
    if (res.status === 0) {
      let str = '';
      res.data.forEach( item => {
        str += `
        
          <div class="kr_comment_card">
            <div class="img-wrap">
              <img src="./uploads/avatar_3.jpg" alt="">
            </div>
            <div class="info">
              <p>${item.uname} · <span>${item.cdate}</span></p>
              <p>${item.content}</p>
            </div>
            <a href="javascript:;" class="like">${item.count}</a>
          </div>
        `
      })

      $('#pinglun').html(str)
    }
  }
})

}
pinglun();
// -----------   -------发表评论----------------------------

$('#comment-form').submit(function (e) {
  e.preventDefault()
  let data = $(this).serialize()
  $.ajax({
    type: 'post',
    url: 'http://localhost:8888/api/articles/' + id + '/comments',
    data: data,
    success: function (res) {
      if (res.status === 0) {
        // 发表评论成功
        layer.msg(res.message)
        // 重置表单
        $('#comment-form')[0].reset()
        pinglun();
      }
    }
  })
})
